<?php 
namespace Home\Controller;
class MycollectController extends HomeController
{
    public function collect()
    {
         $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
        $user_id = $_SESSION['user']['id'];
         $m = M('collect');
        $list = $m->field('a.title,a.cover,c.save_time,c.id,c.art_id')
                         ->table('hx_collect c,hx_article a')
                         ->where("c.user_id=$user_id AND c.category_id=1 AND c.art_id = a.id AND a.status = 1")
                         ->page($_GET['p'].',5')
                         ->order('c.id desc')
                         ->select();
                         // V($list);
         $count = $m->table('hx_collect c,hx_article a')
                         ->where("c.user_id=$user_id AND c.category_id=1 AND a.id=c.art_id AND a.status = 1")
                         ->count();// 查询满足要求的总记录数
                         // V($count);

         $p=getpage($m,$count,5);
         // $list=$m->where(array('status'=>'0'))->order('id DESC')->page($_GET['p'].',10')->select();
         $this->list=$list;
         // $this->assign('list',$list);// 赋值数据集
         $this->page=$p->show();

        $this->assign('art',$list);

        $this->display('Mycollect/collect');
    }


    public function getCategory()
    {
        $list=M('collect_category')->field('category_name')->select();
        $this->ajaxReturn($list);
    }

    public function saveArticle()
    {   
        $art_id = $_POST['art_id'];
        $user_id = $_POST['user_id'];
        $data = M('collect');
        $old=$data ->where(array('art_id'=>$art_id,'user_id'=>$user_id,'category_id'=>1))->select();
        if($old) {
            $this->ajaxReturn(false);
            exit;
        }
        $save_time = date('Y-m-d h:i:s');
        $data ->create();
        $info = $data ->add(array('art_id'=>$art_id,'user_id'=>$user_id,'save_time'=>$save_time));
        if($info) {
        $this->ajaxReturn(true);
        } else {
        $this->ajaxReturn(false);
        }
    }
}